package com.woniu.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: CookieUtil
 * @Description: 获取当前登录的用户名的工具类
 * @Author: Yanghan
 * @Date: 2020/3/24 16:04
 */
@Component
public class CookieUtil {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 功能描述: 获取当前登录系统的用户的用户名
     * @Param: []
     * @Return: java.lang.String
     * @Author: yanghan
     * @Date: 2020/3/24 16:05
     */
    public String getName(){
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for(Cookie cook:cookies){
                String value = cook.getValue();
                String name = cook.getName();
                String RedisName = (String)redisTemplate.boundValueOps(name).get();
                if (RedisName!=null&&RedisName.equals(value)){
                    return RedisName;
                }
            }
        }
        return null;
    }


}
